package com.suse.service.community.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HtmlUtil;
import com.suse.common.util.extra.DesensitizationUtil;
import com.suse.service.community.cache.CacheManager;
import com.suse.service.community.entity.DiscussPost;
import com.suse.service.community.mapper.DiscussPostMapper;
import com.suse.service.community.service.DiscussPostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讨论发布业务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-29 15:33
 */
@Slf4j
@Service
@CacheConfig(cacheNames = "caffeineCacheManager")
public class DiscussPostServiceImpl implements DiscussPostService {

    @Resource
    private DiscussPostMapper discussPostMapper;

    @Resource
    private CacheManager cacheManager;

    @Override
    public List<DiscussPost> getDiscussPostList(int userId, int offset, int limit, int order) {
        if (order == 1) {
            return getHotPostList(userId,offset,limit);
        }else {
            return discussPostMapper.selectList(userId,offset,limit,order);
        }
    }

    @Cacheable(key = "#offset + ':' + #limit")
    public List<DiscussPost> getHotPostList(int userId, int offset, int limit) {
        log.info("正在从数据库中查询热帖列表...");
        return discussPostMapper.selectList(userId,offset,limit,1);
    }

    @Cacheable(key = "#userId")
    @Override
    public int getDiscussPostTotal(int userId) {
        log.info("正在从数据库中查询热帖总数...");
        return discussPostMapper.selectTotal(userId);
    }

    @Override
    public boolean save(DiscussPost discussPost) {
        if (ObjectUtil.isNull(discussPost)) {
            return false;
        }
        // 转义 HTML 标签
        discussPost.setTitle(HtmlUtil.escape(discussPost.getTitle()));
        discussPost.setContent(HtmlUtil.escape(discussPost.getContent()));
        // 过滤敏感词
        discussPost.setTitle(DesensitizationUtil.desensitize(discussPost.getTitle()));
        discussPost.setContent(DesensitizationUtil.desensitize(discussPost.getContent()));
        discussPost.setType(0);
        discussPost.setStatus(0);
        discussPost.setCommentCount(0);
        discussPost.setScore(0.0);
        int row = discussPostMapper.insert(discussPost);
        return row == 1;
    }

    @Override
    public DiscussPost getById(int id) {
        return discussPostMapper.selectOne(id);
    }

    @Override
    public boolean updateCommentCount(int id, int commentCount) {
        int row = discussPostMapper.updateCommentCount(id, commentCount);
        return row == 1;
    }

    @Override
    public void updateType(int id, int type) {
        discussPostMapper.updateType(id,type);
    }

    @Override
    public void updateStatus(int id, int status) {
        discussPostMapper.updateStatus(id,status);
    }

    @Override
    public void updateScore(int id, double score) {
        discussPostMapper.updateScore(id,score);
    }

}
